<template>
  <account-wrap :tips="tips" :submit-text="submitText" :submitHandle="submitHandle">
    <div class="avtar">
      <img :src="avtar" />
    </div>
    <el-row>
      <el-col :span="18" :offset="3">
        <el-form :model="form" :rules="rules" ref="login">
          <el-form-item prop="username">
            <el-row>
              <el-col :span="4"><i class="material-icons">person</i></el-col>
              <el-col :span="20">
                <el-input type="text" v-model="form.username" placeholder="输入昵称/邮箱"></el-input>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item prop="password">
            <el-row>
              <el-col :span="4"><i class="material-icons">lock_outline</i></el-col>
              <el-col :span="20">
                <el-input type="password" v-model="form.password" placeholder="输入密码"></el-input>
              </el-col>
            </el-row>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

  </account-wrap>
</template>

<script>
import accountWrap from './conWrap';
import avtar from './images/avtar.png';

export default {
  name: 'Login',
  data() {
    return {
      avtar,
      tips: {
        content: '点击进行注册',
        icon: 'nature_people',
        link: '/register',
      },
      submitText: '登录',
      form: {},
      rules: {
        username: [{
          required: true,
          message: '请输入账户',
          trigger: 'blur',
        }],
        password: [{
          required: true,
          message: '请输入密码，6-20字符',
          min: 6,
          max: 20,
          trigger: 'blur',
        }],
      },
    };
  },
  methods: {
    submitHandle() {
      this.$refs.login.validate((valid) => {
        if (valid) {
          const { username, password } = this.form;
          this.$store.dispatch('login', {
            username, password,
          })
            .then(() => {
              this.$notify({
                title: '成功',
                message: '登录成功',
                type: 'success',
              });
              this.$router.push('/home/square');
            });
        } else {
          return false;
        }
      });
    },
  },
  components: {
    accountWrap,
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="sass">
@import "./style.scss";
</style>

